<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()" label-width="80px">

      <el-row>   
        <el-col :span="7">
          <el-form-item label="客户">
            <el-select v-model="dataForm.customerNum" placeholder="客户名称">
              <el-option
                v-for="item in customerData"
                :key="item.code"
                :label="item.name"
                :value="item.code">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="订单状态">
            <el-select v-model="dataForm.orderStatus" placeholder="订单状态">
              <el-option
                v-for="item in orderStatusData"
                :key="item.code"
                :label="item.name"
                :value="item.code">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="生产状态">
            <el-select v-model="dataForm.productStatus" placeholder="生产状态">
              <el-option
                v-for="item in productStatusData"
                :key="item.code"
                :label="item.name"
                :value="item.code">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="3"></el-col>
      </el-row>
      <el-row>   
        <el-col :span="16">
          <el-form-item label="作成日">
            <el-date-picker
              v-model="dataForm.createDateFrom"
              value-format="yyyy-MM-dd HH:mm:ss"
              align="right"
              type="date"
              placeholder="作成日开始"
            >
            </el-date-picker>
            <span>~</span>
            <el-date-picker
              v-model="dataForm.createDateTo"
              value-format="yyyy-MM-dd HH:mm:ss"
              align="right"
              type="date"
              placeholder="作成日结束"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8"  align="right">
          <el-form-item>
            <el-button @click="getDataList()">查询</el-button>
            <el-button v-if="isAuth('order:mesordermangemento:save')" type="primary" @click="addOrUpdateHandle('mesordermangemento-add')">新增</el-button>
            <el-button v-if="isAuth('order:mesordermangemento:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      
    </el-form>
    <el-table
      :data="dataList"
      border
      height="380"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        :selectable="checkboxT"
        width="50">
      </el-table-column>
      <el-table-column
        prop="orderNum"
        header-align="center"
        align="center"
        width="200"
        label="订单编号">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle('mesordermangemento-update',scope.row.orderNum)">{{scope.row.orderNum}}</el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="customerName"
        header-align="center"
        width="150"
        align="center"
        label="客户">
      </el-table-column>
      <el-table-column
        prop="quotationRef"
        header-align="center"
        width="120"
        align="center"
        label="参考报价">
      </el-table-column>
      <el-table-column
        prop="quotationActual"
        header-align="center"
        width="120"
        align="center"
        label="实际报价">
      </el-table-column>
      <el-table-column
        prop="deliveryRef"
        header-align="center"
        align="center"
        width="120"
        :formatter="dateFormat"
        label="参考纳期">
      </el-table-column>
      <el-table-column
        prop="deliveryActual"
        header-align="center"
        align="center"
        width="120"
        :formatter="dateFormat"
        label="实际纳期">
      </el-table-column>
      <el-table-column
        prop="orderKeeper"
        header-align="center"
        align="center"
        label="责任人">
      </el-table-column>
      <el-table-column
        prop="createDate"
        header-align="center"
        align="center"
        width="120"
        :formatter="dateFormat"
        label="作成日">
      </el-table-column>
      <el-table-column
        prop="orderStatusName"
        header-align="center"
        align="center"
        label="订单状态">
      </el-table-column>
      <el-table-column
        prop="effectiveStartDate"
        header-align="center"
        width="120"
        align="center"
        :formatter="dateFormat"
        label="有效开始日">
      </el-table-column>
      <el-table-column
        prop="productionStatusName"
        header-align="center"
        align="center"
        label="生产状态">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle('mesordermangemento-update',scope.row.orderNum)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.orderNum)" :disabled="scope.row.orderStatusName=='有效'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dataForm: {
        customerNum: null,
        orderStatus: null,
        productStatus: null,
        createDateFrom: null,
        createDateTo: null
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      customerData: [{ code: '', name: '' }],
      orderStatusData: [{ code: '', name: '' }],
      productStatusData: [{ code: '', name: '' }],
      productionData: [{ code: '', name: '' }]
    }
  },
  components: {},
  activated() {
    this.optionLists()
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/order/mesordermangemento/list'),
        method: 'get',
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          customerNum: this.dataForm.customerNum,
          orderStatus: this.dataForm.orderStatus,
          productStatus: this.dataForm.productStatus,
          createDateFrom: this.dataForm.createDateFrom,
          createDateTo: this.dataForm.createDateTo
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(name, id) {
      this.$router.push({ name: name, params: { id: id } })
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.orderNum
          })
      this.$confirm(
        `确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl('/order/mesordermangemento/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    optionLists() {
      let arr = ['product_status', 'order_status']
      this.$http({
        url: this.$http.adornUrl('/basic/mescodemasterm/dataList'),
        method: 'post',
        data: this.$http.adornParams(arr, false)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.productStatusData = [{ code: '', name: '' }]
          this.orderStatusData = [{ code: '', name: '' }]
          data.product_status.forEach((item, index) => {
            let obj = {}
            obj.code = item.code
            obj.name = item.name
            this.productStatusData.push(obj)
          })
          data.order_status.forEach((item, index) => {
            let obj = {}
            obj.code = item.code
            obj.name = item.name
            this.orderStatusData.push(obj)
          })
        }
      })

      this.$http({
        url: this.$http.adornUrl('/basic/mescustomerm/list'),
        method: 'get',
        params: this.$http.adornParams({
          limit: -1
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.customerData = [{ code: '', name: '' }]
          data.page.list.forEach((item, index) => {
            let obj = {}
            obj.code = item.customerNum
            obj.name = item.customerName
            this.customerData.push(obj)
          })
        }
      })

      this.$http({
        url: this.$http.adornUrl('/basic/mesproductioninfob/list'),
        method: 'get',
        params: this.$http.adornParams({
          limit: -1
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.productionData = [{ code: '', name: '' }]
          data.page.list.forEach((item, index) => {
            let obj = {}
            obj.code = item.productionNum
            obj.name = item.productionName
            this.productionData.push(obj)
          })
        }
      })
    },
    //时间格式化
    dateFormat(row, column, cellValue, index) {
      const daterc = row[column.property]
      if (daterc != null) {
        const dateMat = new Date(daterc.replace("-","/"))
        const year = dateMat.getFullYear()
        const month =
          dateMat.getMonth() + 1 > 10
            ? dateMat.getMonth() + 1
            : '0' + (dateMat.getMonth() + 1)
        const day =
          dateMat.getDate() > 10 ? dateMat.getDate() : '0' + dateMat.getDate()
        const timeFormat = year + '-' + month + '-' + day
        return timeFormat
      }
    },
    // 表格复选有效状态框禁用
    checkboxT(row, index) {
      if (row.orderStatusName === '有效') {
        return 0
      }
      return 1
      // if (row.orderNum == '有效') {
      //   return 0
      // } else {
      //   return 1
      // }
    }
  }
}
</script>
<style lang="scss" scoped>
</style>